1
|
1 |
|
var format = require('string-template'); |
2
|
1 |
|
var handlebars = require('handlebars'); |
3
|
1 |
|
var hljs = require('highlight.js'); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Formats a mixin using a SassDoc mixin object to look like this: |
7
|
|
|
* ```scss |
8
|
|
|
* @include mixinName($param, $param) { } |
9
|
|
|
* ``` |
10
|
|
|
* |
11
|
|
|
* @param {object} mixin - Mixin data, taken from a SassDoc object. |
12
|
|
|
* @returns A formatted code sample. |
13
|
|
|
*/ |
14
|
1 |
|
handlebars.registerHelper('writeSassMixin', function(mixin) { |
15
|
3 |
|
var name = mixin['context']['name']; |
16
|
3 |
|
var params = mixin['parameter']; |
17
|
|
|
|
18
|
3 |
|
var str = '@include '; |
19
|
3 |
|
str += name; |
20
|
|
|
|
21
|
3 |
|
if (params) str += '('; |
|
|
|
|
22
|
|
|
|
23
|
3 |
|
for (var i in params) { |
|
|
|
|
24
|
2 |
|
str += '$' + params[i]['name'] + ', '; |
25
|
|
|
} |
26
|
|
|
|
27
|
3 |
|
if (params) str = str.slice(0, -2) + ')'; |
|
|
|
|
28
|
|
|
|
29
|
3 |
|
if (typeof mixin.content === 'string') { |
30
|
1 |
|
str += ' { }'; |
31
|
|
|
} |
32
|
|
|
else { |
33
|
2 |
|
str += ';' |
34
|
|
|
} |
35
|
|
|
|
36
|
3 |
|
str = hljs.highlight('scss', str).value; |
37
|
|
|
|
38
|
3 |
|
return new handlebars.SafeString(str); |
39
|
|
|
}); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Formats a function using a SassDoc function object to look like this: |
43
|
|
|
* ```scss |
44
|
|
|
* function($param, $param) |
45
|
|
|
* ``` |
46
|
|
|
* |
47
|
|
|
* @param {object} func - Function data, taken from a SassDoc object. |
48
|
|
|
* @returns {string} A formatted code sample. |
49
|
|
|
*/ |
50
|
1 |
|
handlebars.registerHelper('writeSassFunction', function(func) { |
51
|
2 |
|
var name = func['context']['name']; |
52
|
2 |
|
var params = func['parameter']; |
53
|
|
|
|
54
|
2 |
|
var str = ''; |
55
|
2 |
|
str += name + '('; |
56
|
|
|
|
57
|
2 |
|
for (var i in params) { |
|
|
|
|
58
|
2 |
|
str += '$' + params[i]['name'] + ', '; |
59
|
|
|
} |
60
|
2 |
|
if (params) str = str.slice(0, -2); |
|
|
|
|
61
|
2 |
|
str += ')'; |
62
|
|
|
|
63
|
2 |
|
str = hljs.highlight('scss', str).value; |
64
|
|
|
|
65
|
2 |
|
return new handlebars.SafeString(str); |
66
|
|
|
}); |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Formats a variable declaration using a SassDoc variable object to look like this: |
70
|
|
|
* ```scss |
71
|
|
|
* $name: $value; |
72
|
|
|
* ``` |
73
|
|
|
* |
74
|
|
|
* @param {object} variable - Variable data, taken from a SassDoc object. |
75
|
|
|
* @returns {string} A formatted code sample. |
76
|
|
|
*/ |
77
|
1 |
|
handlebars.registerHelper('writeSassVariable', function(variable) { |
78
|
1 |
|
var name = variable['context']['name']; |
79
|
1 |
|
var value = variable['context']['value']; |
80
|
1 |
|
var str = '$' + name + ': ' + value + ';'; |
81
|
1 |
|
str = hljs.highlight('scss', str).value; |
82
|
|
|
|
83
|
1 |
|
return new handlebars.SafeString(str); |
84
|
|
|
}); |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Formats SassDoc "type" definitions to read "x or y or z" |
88
|
|
|
* @param {string[]} types - Array of types. |
89
|
|
|
* @returns {string} A formatted string. |
90
|
|
|
*/ |
91
|
1 |
|
handlebars.registerHelper('formatSassTypes', function(types) { |
92
|
3 |
|
if (typeof types === 'undefined') return ''; |
|
|
|
|
93
|
|
|
|
94
|
2 |
|
var types = types.replace(' ', '').split('|'); |
95
|
2 |
|
var output = ''; |
96
|
|
|
|
97
|
2 |
|
for (var i in types) { |
|
|
|
|
98
|
3 |
|
output += types[i] + ' or '; |
99
|
|
|
} |
100
|
|
|
|
101
|
2 |
|
return output.slice(0, -4); |
102
|
|
|
}); |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Format a Sass value: |
106
|
|
|
* - For basic values, return as-is. |
107
|
|
|
* - For maps, render each item in the map on its own line. |
108
|
|
|
* - For undefined values, return "None" |
109
|
|
|
* @param {string} value - Sass value to format. |
110
|
|
|
* @returns {string} A formatted value. |
111
|
|
|
*/ |
112
|
1 |
|
handlebars.registerHelper('formatSassValue', function(value) { |
113
|
3 |
|
if (typeof value === 'undefined') return '<span style="color: #999;">None</span>'; |
|
|
|
|
114
|
|
|
|
115
|
4 |
|
if (value[0] === '(' && value[value.length - 1] === ')') { |
116
|
1 |
|
value = value.slice(1, -1).split(',').join('<br>'); |
117
|
|
|
} |
118
|
|
|
|
119
|
2 |
|
return value; |
120
|
|
|
}); |
121
|
|
|
|
122
|
|
|
// Adds an external link, pulled from a SassDock @link annotation. |
123
|
1 |
|
handlebars.registerHelper('writeSassLink', function(link) { |
124
|
2 |
|
if (!link) return ''; |
|
|
|
|
125
|
|
|
|
126
|
1 |
|
var output = format('<p><strong>Learn more:</strong> <a href="{0}">{1}</a></p>', [link[0].url, link[0].caption]); |
127
|
|
|
|
128
|
1 |
|
return new handlebars.SafeString(output); |
129
|
|
|
}); |
130
|
|
|
|
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.